package org.groovymud.shell.security.loginsteps;

import static org.groovymud.shell.security.loginsteps.CallBackDelegate.createCallbackArray;

import java.io.IOException;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.login.FailedLoginException;

import org.groovymud.shell.security.MudLoginModuleData;
import org.groovymud.shell.security.callbacks.SelfHandlingNameCallback;

public class RequestUsername extends LoginStep {

	private String usernamePrompt = "username:";

    @Override
	public void execute(MudLoginModuleData data) throws IOException, UnsupportedCallbackException, FailedLoginException {
        NameCallback nameCallback = new SelfHandlingNameCallback(usernamePrompt + " ");
		Callback[] cb = createCallbackArray(nameCallback);

		data.getCallbackHandler().handle(cb);

		data.setUsername(nameCallback.getName().toLowerCase());
		if (getNextStep() != null) {
			getNextStep().execute(data);
		}
	}

}
